import 'dart:async';
import 'package:aliface/model/init_sdk_data.dart';
import 'package:aliface/model/verifier_data.dart';
import 'package:aliface/model/verifier_result.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

export 'model/verifier_data.dart';
export 'model/init_sdk_data.dart';
export 'model/verifier_result.dart';
export 'package:aliface/aliface.dart';

class Aliface {
  static const MethodChannel _channel = MethodChannel('aliface');

  // 初始化人脸识别库
  static Future<InitSdkData> get registerFace async {
    final result = await _channel.invokeMethod('registerFace');
    final bool code = result['code'];
    final String msg = result['msg'];
    return InitSdkData(code: code, msg: msg);
  }

  // 获取metaInfo
  static Future<String> get getMetaInfo async {
    final result = await _channel.invokeMethod('getMetaInfo');
    final metaInfo = result['metaInfo'] ?? "";
    return metaInfo;
  }

  // 开始实人认证
  static Future<VerifierData> startVerifier({required String certifyId}) async {
    final params = {'certifyId': certifyId};
    final result = await _channel.invokeMethod('startVerifier', params);
    final int code = result["code"];
    String msg = result["msg"] ?? "";
    if (kDebugMode) {
      print("msg是否为空===>${msg.isNotEmpty}");
    }
    if (code == 1000) {
      // 刷脸成功
      return VerifierData(
          type: VerifierResult.success,
          code: code,
          msg: msg.isNotEmpty ? msg : "刷脸验证成功");
    } else if (code == 1001) {
      // 系统错误
      return VerifierData(
          type: VerifierResult.exception,
          code: code,
          msg: msg.isNotEmpty ? msg : "系统错误");
    } else if (code == 1003) {
      // 验证中断
      return VerifierData(
          type: VerifierResult.discontinue,
          code: code,
          msg: msg.isNotEmpty ? msg : "验证中断");
    } else if (code == 2002) {
      // 网络错误
      return VerifierData(
          type: VerifierResult.networkError,
          code: code,
          msg: msg.isNotEmpty ? msg : "网络错误");
    } else if (code == 2003) {
      // 客户端设备时间错误
      return VerifierData(
          type: VerifierResult.appTimeError,
          code: code,
          msg: msg.isNotEmpty ? msg : "客户端设备时间错误");
    } else if (code == 2006) {
      // 刷脸失败
      return VerifierData(
          type: VerifierResult.failed,
          code: code,
          msg: msg.isNotEmpty ? msg : "刷脸失败");
    } else {
      // 未知错误
      return VerifierData(
          type: VerifierResult.no,
          code: code,
          msg: msg.isNotEmpty ? msg : "其他错误");
    }
  }
}
